package com.df.springmvc.supper;

import java.net.URLDecoder;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.common.bean.DhtmlGridBean;
import com.df.springmvc.common.bean.CurrentUserBean;
import com.df.springmvc.common.util.CheckSqlInjectUtil;

public abstract class SupperController implements SupperControllerI{
	public String path;
	public String webPath;
	{
		this.getClass().getResource("/").getHost();
		path=URLDecoder.decode(SupperController.class.getClassLoader().getResource("").getPath());
		webPath=path.replace("\\","/").replace("/WEB-INF/classes","");
	}
	final static public String Add="Add";
	final static public String Upd="Upd";
	final static public String Del="Del";
	final static public String Config="Config";
	final static public String Query="query";
	final static public String View="View";
	final static public String toMain="";
	final static public String toAdd="toAdd";
	final static public String toUpd="toUpd";
	final static public String toDel="toDel";
	final static public String toView="toView";
	final static public String toConfig="toConfig";
	final static public String doAdd="doAdd";
	final static public String doUpd="doUpd";
	final static public String doDel="doDel";
	final static public String doView="doView";
	final static public String doConfig="doConfig";
	final static public String page_gridId="gridId_";
	final static public String page_current="CurrentPageNum_";
	final static public String page_size="PageSize_";
	final static public String act_form_guid="act_form_guid";
	final static public String act_form_taskId="act_form_taskId";
	
	public HttpServletRequest getRequest(){
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	public HttpSession getSession(){
		return getRequest().getSession();
	}
	public void addRequestAttribute(String key,Object value){
		getRequest().setAttribute(key,value);
	}
	public void addSessionAttribute(String key,Object value){
		getSession().setAttribute(key,value);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args){
		// TODO Auto-generated method stub
	}
	public static String getParam_CurrentPageNum(HttpServletRequest request){
		String temp=request.getParameter(page_current);
		return temp == null?"0":temp;
	}
	public static String getParam_gridId(HttpServletRequest request){
		String temp=request.getParameter(page_gridId);
		return temp == null?"0":temp;
	}
	public static String getParam_PageSize(HttpServletRequest request){
		String temp=request.getParameter(page_size);
		return temp == null?"0":temp;
	}
	public static DhtmlGridBean getParam_PagingBean(HttpServletRequest request){
		DhtmlGridBean bean=new DhtmlGridBean();
		bean.setCurrentPageNum(Integer.parseInt(getParam_CurrentPageNum(request)));
		bean.setPageSize(Integer.parseInt(getParam_PageSize(request)));
		bean.setGridId(getParam_gridId(request));
		bean.setSessionId(request.getSession().getId());
		return bean;
	}
	/**
	 * @param request
	 * @return
	 */
	public static HashMap<String,String> getParamMap(HttpServletRequest request){
		return getParamMap(request,"");
	}
	/**
	 * @param request
	 * @param prefix
	 * @return
	 */
	public static HashMap<String,String> getParamMap(HttpServletRequest request,String prefix){
		HashMap<String,String> map=new HashMap<String,String>();
		for(String key:request.getParameterMap().keySet()){
			if(key.contains(prefix)){
				map.put(key.replace(prefix + ".","").replace("[]",""),getParam(request,key));
			}
		}
		return map;
	}
	public static String getParam(HttpServletRequest request,String key){
		String value="";
		String[] tmp=request.getParameterValues(key);
		if(tmp == null){
			return "";
		}else if(tmp.length == 1){
			value=tmp[0];
		}else{
			for(String t:tmp){
				value+="," + t;
			}
		}
		if(value.startsWith(",")){
			value=value.replaceFirst(",","");
		}
		value=CheckSqlInjectUtil.replace(value);
		System.out.println(key + ":" + value);
		return value;
	}
	/**
	 * 获取当时用户
	 * @return
	 */
	public CurrentUserBean getCurrentUserBean(){
		if(getSession().getAttribute("CurrentUserBean") != null){
			return (CurrentUserBean)getSession().getAttribute("CurrentUserBean");
		}else{
			return new CurrentUserBean();
		}
	}
	// ////////////////////////////////
	@Override
	@RequestMapping(value={"/"})
	public String excute(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		return "";
	}
	@Override
	@RequestMapping(value={"/" + toAdd})
	public String toAdd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		return Add;
	}
	@Override
	@RequestMapping(value={"/" + toUpd})
	public String toUpd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		return Upd;
	}
	@Override
	@RequestMapping(value={"/" + toDel})
	public String toDel(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		return Del;
	}
	@Override
	@RequestMapping(value={"/" + toView})
	public String toView(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		return View;
	}
	@Override
	@RequestMapping(value={"/" + toConfig})
	public String toConfig(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	@RequestMapping(value={"/" + doAdd})
	public void doAdd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	@Override
	@RequestMapping(value={"/" + doUpd})
	public void doUpd(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	@Override
	@RequestMapping(value={"/" + doDel})
	public void doDel(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	@Override
	@RequestMapping(value={"/" + doView})
	public void doView(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	@Override
	@RequestMapping(value={"/" + doConfig})
	public void doConfig(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	@Override
	@RequestMapping(value={"/" + Query})
	public void query(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	@Override
	@RequestMapping(value={"/checkLogin"})
	public void checkLogin(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	// ////////////////////////////
	@Override
	@RequestMapping(value={"/toXXX"})
	public String toXXX(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		return "XXX";
	}
	@Override
	@RequestMapping(value={"/doXXX"})
	public void doXXX(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	@Override
	@RequestMapping(value={"/queryXXX"})
	public void queryXXX(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
	}
	@Override
	@RequestMapping(value={"/doExport"})
	public void export(ModelMap model,HttpServletRequest request,HttpServletResponse response){
		// TODO Auto-generated method stub
		try{
			DhtmlGridBean dhtmlGridBean=getParam_PagingBean(request);
			dhtmlGridBean.exportAllPage(request,response);
		}catch(Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
